local quanji = fk.CreateSkill {
  name = "hx__quanji",
  derived_piles = "hxzhaohua__zhonghui_power",
}


local spec = {
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local choice = room:askToChoice(player, {
      skill_name = quanji.name,
      choices = {"hx__quanji-1", "hx__quanji-2"},
      prompt = "#hx__quanji-draw",
      cancelable = true,
    })
    local num
    if choice then
      if choice == "hx__quanji-1" then
        num = 1
      elseif choice == "hx__quanji-2" then
        num = 2
      end
      event:setCostData(self, {num = num})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local num = event:getCostData(self).num
    player:drawCards(num, quanji.name)
    if num == 2 then return end
    if player:isNude() or player.dead then return end
    local card = room:askToCards(player, {
      skill_name = quanji.name,
      include_equip = true,
      min_num = 1,
      max_num = #player:getCardIds("he"),
      prompt = "#hx__quanji",
      cancelable = false,
    })
    player:addToPile("hxzhaohua__zhonghui_power", card, true, quanji.name)
  end,
}

quanji:addEffect(fk.EventPhaseEnd, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(quanji.name) and player.phase == Player.Play
  end,
  on_cost = spec.on_cost,
  on_use = spec.on_use,
})


quanji:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(quanji.name) then
      for _, move in ipairs(data) do
        if move.from == player and move.to and move.to ~= player and move.moveReason == fk.ReasonPrey then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerEquip or info.fromArea == Card.PlayerHand then
              return true
            end
          end
        end
      end
    end
  end,
  on_cost = spec.on_cost,
  on_use = spec.on_use,
})

quanji:addEffect(fk.Damaged, {
  anim_type = "masochism",
  trigger_times = function(self, event, target, player, data)
    return data.damage
  end,
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(quanji.name)
  end,
  on_cost = spec.on_cost,
  on_use = spec.on_use,
})

quanji:addEffect("maxcards", {
  correct_func = function(self, player)
    if player:hasSkill(quanji.name) then
      return #player:getPile("hxzhaohua__zhonghui_power")
    end
  end,
})


return quanji
